<?php

namespace app\api\controller\v25;

use app\api\controller\v2\Auth;
use app\common\cachemodel\Orders as OrdersCacheModel;
use app\common\cachemodel\OrdersLib as OrdersCacheModelLib;
use app\common\cachemodel\RectificationEvent;
use app\common\cachemodel\ZhunleUserAccount;
use app\common\lib\Util;
use Exception;
use think\Log;
use think\response\Json;

/**
 * @version 2.6.0 订单控制器
 * Class Orders
 * @package app\api\controller\v13
 */
class Orders extends Auth
{
    public $msg;

    /**
     * 接口，获取订单详情
     * @throws Exception
     */
    public function read()
    {
        $this->msg = '';
        $order_id = input('param.order_id', '');
        if (!$order_id)
        {
            return show(config('code.error'), '未知的订单');
        }
        $ordersCacheModel = new OrdersCacheModel();
        $orders = current($ordersCacheModel->batchGetInfo([$order_id]));
        if ($orders)
        {
            if ($orders['uid'] != $this->user['id'])
            {
                return show(config('code.error'), '订单不存在');
            }
            try
            {
                $info = $this->read_data_format($orders);
            }
            catch (\Exception $e)
            {
                Log::write('订单详情异常：' . $e->getMessage() . '错误文件和行数：' . $e->getFile() . ':' . $e->getLine(), 'error', true);

                return show(config('code.error'), '服务异常，请稍后重试');
            }

            return show(config('code.success'), '获取成功', $info);
        }

        return show(config('code.error'), '订单不存在');
    }

    /**
     * 组合订单详情数据格式
     * @param $orders
     * @return array
     * @throws Exception
     */
    private function read_data_format($orders)
    {
        $ordersModel = new OrdersCacheModel();
        $recodeModel = new \app\common\cachemodel\Recode();
        $orders_details = $ordersModel->ordersDetail($orders);
        //订单数据重组
        $info = [
            'status_des' => $orders_details['mini_type_des'] ? $orders_details['status_des'] . $orders_details['mini_type_des'] : $orders_details['status_des'],
            'order_icon' => $this->getReadOrderBtn($orders_details)['icon'],
            'total_amount' => $orders_details['total_amount'],
            'trade_type' => (int)$orders_details['trade_type'],
            'order_id' => $orders_details['order_id'],
            'order_btn' => $this->getReadOrderBtn($orders_details)['btn'],
            'is_display_recommend_data' => $orders_details['is_display_recommend_data'],
            'des' => $orders_details['des'],
            'time_remaining' => $orders_details['time_remaining'],
            'coupon_data' => $orders_details['coupon_data'],
            'active_data' => $orders_details['active_data'],
            'coin_num' => OrdersCacheModelLib::getCoinNum($this->user['id']),
            'goods_data' => [],
            'recode_data' => NULL,
            'rectification_data' => NULL,
            'refund_data' => $orders_details['refund_data'],
            'note_tips' => '',
            'gid' => (int)($orders_details['gid'] ?? 0),
            'rid' => 0
        ];

        // 待支付返回准币数量及是否设置准币支付密码
        if ($orders_details['status'] == 0)
        {
            $user_coin = (new ZhunleUserAccount())->getUserCoinFromCache($this->user['id']);
            $info['coin_num'] = (int)($user_coin['balance'] ?? 0);
            $info['pwd_status'] = (isset($user_coin['password']) && $user_coin['password']) ? 1 : 0;
        }

        $info['order_data'][] = [
            'name' => '下单时间',
            'value' => date('Y-m-d H:i:s', $orders_details['create_time']),
        ];
        // 苹果支付不显示支付方式
        if ((int)$orders_details['trade_type'] !== 9)
        {
            $info['order_data'][] = [
                'name' => '支付方式',
                'value' => $orders_details['trade_type_des'],
            ];
        }
        if ($orders_details['pay_time'])
        {
            $info['order_data'][] = [
                'name' => '支付时间',
                'value' => date('Y-m-d H:i:s', $orders_details['pay_time']),
            ];
        }

        //重组购买的商品信息
        switch (true)
        {
            case (int)$orders_details['order_type'] === 1://生时矫正
                $detail = $ordersModel->RecRead($orders_details);
                //获取档案信息
                $recode = current($detail['recode_data']);
                $info['recode_data']['des'] = $recode['name'];
                $info['recode_data']['info'][] = $recodeModel->recodeInfoFormat($recode);
                //获取校正事件信息
                $info['rectification_data']['des1'] = date('Y-m-d', $detail['data']['start_time']);
                $info['rectification_data']['des2'] = date('H:i', $detail['data']['start_time']) . '至' . ((date('d', $detail['data']['start_time']) != date('d', $detail['data']['end_time'])) ? '次日' : '') . date('H:i', $detail['data']['end_time']);
                //获取校准生辰事件信息
                $events = (new RectificationEvent())->batchGetInfoByIds($orders_details['reid'], [
                    'id',
                    'status',
                    'reid',
                    'time',
                    'type',
                    'desc',
                ]);
                foreach ($events as $k => $event)
                {
                    unset($event['status'], $event['reid']);
                    $event['time'] = $event['time'] ? date('Y-m', $event['time']) : '';
                    $events[$k] = $event;
                }
                //获取商品信息
                $goodsData['price'] = current($detail['goods_data'])['now_prize'];
                $goodsData['name'] = '校准生辰服务';
                $goodsData['des'] = '';
                $goodsData['rname'] = $recode['name'];
                $info['rectification_data']['event'] = $events;
                if ($recode['status'])
                {
                    $targetInfo = $this->getTargetLink($orders_details);
                    $goodsData['target'] = $targetInfo['target'];
                    $goodsData['link'] = $targetInfo['link'];
                } else
                {
                    $goodsData['target'] = 'toast';
                    $goodsData['link'] = "当前档案已删除\r\n不支持查看"; // 如果档案删除了 则不跳转
                }
                $info['goods_data'][] = $goodsData;
                $info['rid'] = $recode['id'];
                break;
            case (int)$orders_details['order_type'] === 2://日运
            case (int)$orders_details['order_type'] === 3://月运
            case (int)$orders_details['order_type'] === 4://年运
                $detail = $ordersModel->FortuneRead($orders_details);
                $goodsData = [];
                //获取商品信息
                if ($orders_details['order_type'] == 2)
                {
                    $goodsData['name'] = '每日运势开通';
                } else if ($orders_details['order_type'] == 3)
                {
                    $goodsData['name'] = '月度运势开通';
                } else if ($orders_details['order_type'] == 4)
                {
                    $goodsData['name'] = '年度运势开通';
                } else
                {
                    $goodsData['name'] = '运势开通';
                }
                //获取商品信息
                $goodsData['price'] = current($detail['goods_data'])['now_prize'];
                $goodsData['des'] = '';
                $goodsList = [];
                foreach ($detail['recode_data'] as $recode_datum)
                {
                    if (!isset($recode_datum['status']))
                    {
                        $recodeStatus = (new \app\common\cachemodel\Recode())->getInfoById($recode_datum['id'], ['status']);
                        $recode_datum['status'] = isset($recodeStatus['status']) ? (int)$recodeStatus['status'] : 0;
                    }
                    //获取档案信息
                    $info['recode_data']['des'] = count($detail['recode_data']) > 1 ? '全部档案' : $recode_datum['name'];
                    $orders_details['time'] = $detail['fortune_data']['start_time'];
                    $orders_details['rid'] = $recode_datum['id'];
                    $orders_details['recode_info'] = $recode_datum;
                    if ((int)$orders['status'] === 4)
                    {
                        $targetInfo = $this->getTargetLink($orders_details);
                    } else
                    {
                        $targetInfo = ['target' => 'local', 'link' => 'empty'];
                    }
                    $goodsList[] = ['rname' => $recode_datum['name'], 'target' => $targetInfo['target'], 'link' => $targetInfo['link']];
                    $info['recode_data']['info'][] = $recodeModel->recodeInfoFormat($recode_datum);
                }
                $info['order_data'][] = [
                    'name' => '开通日期',
                    'value' => $detail['fortune_data']['start_time'] . '至' . $detail['fortune_data']['end_time'],
                ];
                array_walk($goodsList, static function (&$v, $k, $p)
                {
                    $v = array_merge($v, $p);
                }, $goodsData);
                $info['goods_data'] = $goodsList;
                foreach ($detail['recode_data'] as $recodeItem)
                {
                    if (!$recodeItem['status'])
                    {
                        $info['order_btn'] = Util::orderBtnReset($info['order_btn']);
                        continue;
                    }
                }
                break;
            case (int)$orders_details['order_type'] === 5://地图
                $detail = $ordersModel->MapRead($orders_details);
                //获取档案信息
                foreach ($detail['recode_data'] as $recode)
                {
                    $info['rid'] = $recode['id'];
                    $orders_details['rid'] = $recode['id'];
                    $orders_details['recode_status'] = current($recodeModel->getInfoById($recode['id'], ['status']));
                    if ((int)$orders['status'] === 4)
                    {
                        $targetInfo = $this->getTargetLink($orders_details);
                    } else
                    {
                        $targetInfo = ['target' => 'local', 'link' => 'empty'];
                    }
                    $goodsData['rname'] = $recode['name'];
                    $goodsData['target'] = $targetInfo['target'];
                    $goodsData['link'] = $targetInfo['link'];
                    $info['recode_data']['info'][] = $recodeModel->recodeInfoFormat($recode);
                    //获取档案信息
                    $info['recode_data']['des'] = $recode['name'];
                }
                //获取商品信息
                $goodsData['name'] = '幸运之城解锁';
                //获取订单商品价格
                $goodsData['price'] = array_sum(array_map(static function ($val)
                {
                    return $val['now_prize'];
                }, $detail['goods_data']));
                //获取订单商品名称
                $gids = array_keys($detail['goods_data']);
                if (in_array(18, $gids, false))
                {
                    $goodsData['des'] = '开通命运之城';
                } elseif (in_array(19, $gids, false))
                {
                    $goodsData['des'] = '开通全内容打包解锁';
                } else
                {
                    $goodsData['des'] = implode(',', array_map(static function ($val)
                    {
                        return $val['name'];
                    }, $detail['goods_data']));
                }
                $info['goods_data'][] = $goodsData;
                foreach ($detail['recode_data'] as $recodeItem)
                {
                    if (!$recodeItem['status'])
                    {
                        $info['order_btn'] = Util::orderBtnReset($info['order_btn']);
                        continue;
                    }
                }
                break;
            case (int)$orders_details['order_type'] === 6 ://教学视频
                $detail = $ordersModel->VideoRead($orders_details);
                $goods = current($detail['goods_data']);
                //获取商品信息
                $goodsData['name'] = '一生报告解锁';
                $goodsData['price'] = $goods['now_prize'];
                $goodsData['des'] = $detail['goods_data_des'];
                $targetInfo = $this->getTargetLink($orders_details);
                $goodsData['target'] = $targetInfo['target'];
                $goodsData['link'] = $targetInfo['link'];
                $info['goods_data'][] = $goodsData;
                break;
            case (int)$orders_details['order_type'] === 7://本命预测
                $detail = $ordersModel->MapRead($orders_details);
                $recode = current($detail['recode_data']);
                $info['recode_data']['info'][] = $recodeModel->recodeInfoFormat($recode);
                $goodsData = [];
                //获取商品信息
                foreach ($detail['goods_data'] as $goodsDatum)
                {
                    $orders_details['rid'] = $recode['id'];
                    $orders_details['gid'] = $goodsDatum['id'];
                    $orders_details['goods_data'] = $detail['goods_data'];
                    if ((int)$orders['status'] === 4)
                    {
                        $targetInfo = $this->getTargetLink($orders_details);
                    } else
                    {
                        $targetInfo = ['target' => 'local', 'link' => 'empty'];
                    }
                    $goodsData[] = [
                        'rname' => $recode['name'],
                        'name' => '一生报告解锁',
                        'price' => $goodsDatum['now_prize'],
                        'target' => $targetInfo['target'],
                        'link' => $targetInfo['link'],
                        'des' => $goodsDatum['name']
                    ];
                    //获取档案信息
                    $info['recode_data']['des'] = $recode['name'];
                }
                $info['rid'] = $recode['id'];
                $info['goods_data'] = $goodsData;
                foreach ($detail['recode_data'] as $recodeItem)
                {
                    if (isset($recodeItem['status']) && !$recodeItem['status'])
                    {
                        $info['order_btn'] = Util::orderBtnReset($info['order_btn']);
                        continue;
                    }
                }
                break;
            case (int)$orders_details['order_type'] === 8://会员
                $detail = $ordersModel->VipRead($orders_details);
                $goods = current($detail['goods_data']);
                //获取商品信息
                $goodsData['name'] = '开通会员';
                $goodsData['price'] = $goods['now_prize'];
                $goodsData['des'] = $goods['name'];
                $targetInfo = $this->getTargetLink($orders_details);
                $goodsData['target'] = $targetInfo['target'];
                $goodsData['link'] = $targetInfo['link'];
                $info['goods_data'][] = $goodsData;
                //若会员支付成功，则显示有效期
                if ((int)$orders_details['status'] === 4)
                {
                    $info['order_data'][] = [
                        'name' => '开通日期',
                        'value' => $detail['vip_start_time'] . '至' . $detail['vip_end_time'],
                    ];
                }
                break;
            case (int)$orders_details['order_type'] === 15://定制报告
                $customReport = $ordersModel->CustomReport($orders_details);
                $orders_details['qid'] = $customReport['question_data']['id'];
                $orders_details['qstatus'] = $customReport['question_data']['status'];
                //获取商品信息
                $goodsData = [
                    'name' => '定制报告',
                    'price' => $customReport['goods_data']['price'],
                    'des' => '',
                ];
                $targetInfo = $this->getTargetLink($orders_details);
                $goodsData['target'] = $targetInfo['target'];
                $goodsData['link'] = $targetInfo['link'];

                //获取档案信息
                foreach ($customReport['recode_data'] as $recode)
                {
                    if (!$recode)
                    {
                        continue;
                    }
                    $info['rid'] = $recode['id'];
                    $orders_details['rid'] = $recode['id'];
                    $goodsData['rname'] = $recode['name'];
                    $info['recode_data']['info'][] = $recodeModel->recodeInfoFormat($recode);
                    //获取档案信息
                    if ($customReport['question_data']['is_merge'])
                    {
                        $info['recode_data']['des'] = '全部档案';
                    } else
                    {
                        $info['recode_data']['des'] = current($customReport['recode_data'])['name'];
                    }
                }
                $info['goods_data'][] = $goodsData;
                if ($orders_details['status'] == 1 && $customReport['question_data']['status_link']['type'] == 2)
                {
                    // $info['note_tips'] = $customReport['question_data']['status_link']['order_des'];
                    $info['note_tips'] = '注：18:00点前的提问可在6小时内得到回复，18:00—次日08:00之间订单将在次日08:00开始计时';
                }
                if ((int)$orders_details['status'] === 1)
                {
                    $info['des'] = '占星师接单后，6小时内得到回复';
                }
                foreach ($customReport['recode_data'] as $recodeItem)
                {
                    if (!$recodeItem['status'])
                    {
                        $info['order_btn'] = Util::orderBtnReset($info['order_btn']);
                        continue;
                    }
                }
                break;
            case (int)$orders_details['order_type'] === 16://塔罗
                $tarot = $ordersModel->tarot($orders_details);
                $orders_details['qid'] = $tarot['history']['id'];
                $goodsData['name'] = '塔罗占卜';
                $goodsData['price'] = current($tarot['goods_data'])['now_prize'];
                $goodsData['des'] = $tarot['history']['content'];
                $targetInfo = $this->getTargetLink($orders_details);
                $goodsData['target'] = $targetInfo['target'];
                $goodsData['link'] = $targetInfo['link'];
                $info['goods_data'][] = $goodsData;
                break;
            case (int)$orders_details['order_type'] === 17://合盘报告
                $synastryReport = $ordersModel->SynastryReport($orders_details);
                $goods = current($synastryReport['goods_data']);
                $extend = \GuzzleHttp\json_decode($orders['extend'], true);
                $startTime = $extend['synastry_report']['start_time'] ?? NULL;
                if ($startTime)
                {
                    $addYear = $synastryReport['goods_data'][$goods['id']]['expire_time'];
                    $info['order_data'][] = [
                        'name' => '开通时间',
                        'value' => date('Y.m', $startTime) . '至' . date('Y.m', strtotime($addYear . ' -1month', $startTime)),
                    ];
                }
                $srid = $extend['synastry_report']['srid'] ?? 1;
                //获取商品信息
                $goodsData = [
                    'srid' => $srid,
                    'name' => config('synastryreport')[$srid]['good_name'] ?? $goods['name'],
                    'price' => $goods['now_prize'],
                    'des' => '当前关系：' . $synastryReport['relation_data']['name'],
                ];
                if ((int)$orders['status'] === 4)
                {
                    $targetInfo = $this->getTargetLink($orders_details);
                } else
                {
                    $targetInfo = ['target' => 'local', 'link' => 'empty'];
                }
                $goodsData['target'] = $targetInfo['target'];
                $goodsData['link'] = $targetInfo['link'];
                //获取档案信息
                foreach ($synastryReport['recode_data'] as $recode)
                {
                    $orders_details['rid'] = $recode['id'];
                    $goodsData['rname'] = $recode['name'];
                    $info['recode_data']['info'][] = $recodeModel->recodeInfoFormat($recode);
                    //获取档案信息
                    $info['recode_data']['des'] = '全部档案';
                }
                $info['goods_data'][] = $goodsData;
                $info['recode_data']['des'] = implode('&', array_column($synastryReport['recode_data'], 'name'));
                foreach ($synastryReport['recode_data'] as $recodeItem)
                {
                    if (!$recodeItem['status'])
                    {
                        $info['order_btn'] = Util::orderBtnReset($info['order_btn']);
                        continue;
                    }
                }
                break;
            case (int)$orders_details['order_type'] === 18://心理测试
                $detail = $ordersModel->MentalTestDetail($orders_details);
                //获取商品信息
                $goodsData['name'] = $detail['name'];
                $goodsData['des'] = $detail['short_describe'] ?? '';
                $goodsData['img'] = $detail['intro_picture'] ?? '';
                $goodsData['price'] = $detail['price_original'];

                $targetInfo = $this->getTargetLink($orders_details);
                $goodsData['target'] = $targetInfo['target'];
                $goodsData['link'] = $targetInfo['link'];
                $info['goods_data'][] = $goodsData;
                break;
        }

        return $info;
    }

    /**
     * 获取订单跳转地址
     * @param $orders_details
     * @return array
     */
    private function getTargetLink($orders_details)
    {
        if ($orders_details['is_skip'])
        {
            $ordersModel = new OrdersCacheModel();
            $linkInfo = $ordersModel->orderTypeLink($orders_details);
            $info['target'] = $linkInfo['target'];
            $info['link'] = $linkInfo['link'];
        } else
        {
            $info['target'] = 'local';
            $info['link'] = 'empty';
        }
        return $info;
    }

    /**
     * 获取订单详情按钮和图标
     * @param $orders
     * @return mixed
     */
    private function getReadOrderBtn($orders)
    {
        $bans = config('zhunleorder');
        $data['icon'] = $bans['order_icon'][$orders['status']];
        $data['btn'] = $bans['read_order_btn'][$orders['status']];
        if (in_array((int)$orders['status'], [3, 4], true) && in_array((int)$orders['order_type'], [1, 5, 6], true))
        {
            $data['btn'] = [];
        }


        if (18 === (int)$orders['order_type'])
        {//如果为心理测评，那么什么按钮都不显示
            $data['btn'] = [];
        }

        return $data;
    }

    /**
     * 档案订单列表
     * @return Json
     * @throws Exception
     */
    public function recodeOrderList()
    {
        $rid = input('rid/d', 0);
        $order_type = input('order_type/d', 0);
        $page = input('page/d', 1);
        if (!$rid)
        {
            return show(config('code.error'), '请求异常');
        }
        $recode = (new \app\common\cachemodel\Recode())->getInfoById($rid, ['uid', 'name']);
        if (empty($recode) || $recode['uid'] != $this->user['id'])
        {
            return show(config('code.error'), '请求异常');
        }
        $this->msg = '';
        OrdersCacheModelLib::getUserAllUnpaidOrder($this->user['id']);
        $ordersModel = new OrdersCacheModel();
        $orderIds = (new \app\common\service\Orders())->getRecodeOrderIds($this->user['id'], $rid, $order_type);
        $data_list = [];
        if ($orderIds)
        {
            $data_list = getPaginatorData(count($orderIds), $page);
            //获取分页后的数据
            $orderIds = array_slice($orderIds, $data_list['start'], 10);
            unset($data_list['start'], $data_list['end']);
            $orders = $ordersModel->batchGetInfo($orderIds, ['id', 'is_del', 'status', 'order_type', 'uid', 'gid', 'order_id', 'fid', 'reid', 'trade_type', 'total_amount', 'create_time', 'transaction_id', 'pay_time', 'return_time', 'account', 'extend', 'env_type']);
            try
            {
                $ids = array_column($orders, 'id');
                array_multisort($ids, SORT_NUMERIC, SORT_DESC, $orders);
                $orderIds = array_keys($orders);
                $data_list['data'] = array_values((new \app\api\controller\v21\Orders())->resetOrderList($orderIds, $orders));

                foreach ($data_list['data'] as $key => $datum)
                {
                    unset($data_list['data'][$key]['order_btns']);
                }
            }
            catch (Exception $e)
            {
                Log::write('订单列表数据问题：' . $e->getMessage() . '错误文件和行数：' . $e->getFile() . ':' . $e->getLine(), 'error', true);

                return show(config('code.error'), '服务异常，请稍后重试');
            }
        }
        return show(config('code.success'), '加载成功', $data_list);
    }

    /**
     * 档案购买订单数量
     * @return Json
     * @throws Exception
     */
    public function recodeOrderNum()
    {
        $rid = input('rid/d', 0);
        $order_type = input('order_type/d', 0);
        if (!$rid)
        {
            return show(config('code.error'), '请求异常');
        }
        $recode = (new \app\common\cachemodel\Recode())->getInfoById($rid, ['uid', 'name']);
        if (empty($recode) || $recode['uid'] != $this->user['id'])
        {
            return show(config('code.error'), '请求异常');
        }
        OrdersCacheModelLib::getUserAllUnpaidOrder($this->user['id']);
        $orderIds = (new \app\common\service\Orders())->getRecodeOrderIds($this->user['id'], $rid, $order_type);
        $num = count($orderIds);
        if ($num > 0)
        {
            $des = "您已为此档案购买过{$num}笔订单，如仍要删除，请先核对历史订单信息";
        } else
        {
            $des = '此操作不可逆，已删除的档案无法再次恢复哦～';
        }
        return show(config('code.success'), '加载成功', ['title' => '确定删除“' . $recode['name'] . '”的档案吗', 'des' => $des, 'unlock_num' => $num]);
    }

    /**
     * 订单列表数据重构
     * @param $orders
     * @return array
     * @throws Exception
     */
    private function resetOrderList($orders)
    {
        $data = [];
        $ordersModel = new OrdersCacheModel();
        foreach ($orders as $key => $order)
        {
            $order_data = [];
            $order = $ordersModel->ordersDetail($order);
            $data[$key]['order_type'] = $order['order_type'];
            $data[$key]['order_id'] = $order['order_id'];
            $data[$key]['trade_type'] = $order['trade_type'];
            $data[$key]['total_amount'] = $order['total_amount'];
            $data[$key]['create_time'] = date('Y-m-d H:i:s', $order['create_time']);
            $data[$key]['timestamp'] = $order['create_time'];
            $data[$key]['order_status'] = $order['status_des'];
            $data[$key]['mini_type_des'] = $order['mini_type_des'];
            $order_data['des2'] = NULL;//订单描述初始化
            $order_data['img'] = NULL;//订单图片初始化
            switch (true)
            {
                //重组校准生辰订单
                case (int)$order['order_type'] === 1:
                    $rec = $ordersModel->RecRead($order);
                    $data[$key]['order_name'] = '校准生辰';
                    $order_data['title'] = '校准生辰服务';
                    $order_data['des1'] = '出生时间：' . date('Y-m-d', $rec['data']['start_time']);
                    $order_data['des2'] = '时间范围：' . date('H:i', $rec['data']['start_time']) . '-' . ((date('d', $rec['data']['start_time']) != date('d', $rec['data']['end_time'])) ? '次日' : '') . date('H:i', $rec['data']['end_time']);
                    break;
                //重组运势订单
                case in_array((int)$order['order_type'], [2, 3, 4, 12], true):
                    $fortune = $ordersModel->FortuneRead($order);
                    switch ($order['order_type'])
                    {
                        case 2:
                            $order_data['title'] = '每日运势';
                            break;
                        case 3:
                            $order_data['title'] = '月度运势';
                            break;
                        case 4:
                            $order_data['title'] = '年度运势';
                            break;
                        default :
                            $order_data['title'] = '运势';
                            break;
                    }
                    $data[$key]['order_name'] = '运势订单';
                    $recode_name = [];
                    foreach ($fortune['recode_data'] as $recode)
                    {
                        $recode_name[] = $recode['name'];
                        $order_data['des2'] = '开通日期：' . $fortune['fortune_data']['start_time'] . '至' . $fortune['fortune_data']['end_time'];
                    }
                    $frame = $recode_name ? implode(',', $recode_name) : '';
                    $order_data['des1'] = '开通档案：' . $frame;
                    break;
                //重组地图订单
                case (int)$order['order_type'] === 5:
                    $map = $ordersModel->MapRead($order);
                    $data[$key]['order_name'] = '幸运之城';
                    $order_data['title'] = '幸运之城解锁';
                    $map_recode = $map['recode_data'] ? current($map['recode_data']) : [];
                    $game = [];
                    foreach ($map['goods_data'] as $acg)
                    {
                        $game[] = $acg['name'];
                    }
                    $map_count = count($game);
                    $gabe_sta = $game ? implode(',', $game) : '';
                    if ($map_count > 2)
                    {
                        $des = '开通' . implode(',', array_slice($game, 0, 2)) . '等' . $map_count . '条';
                    } else
                    {
                        $des = '开通' . $gabe_sta . '等' . $map_count . '条';
                    }
                    $order_data['des1'] = '购买档案：' . $map_recode['name'] ?? '';
                    $order_data['des2'] = '订单详情：' . $des;
                    break;
                //重组本命预测订单
                case (int)$order['order_type'] === 7:
                    $report = $ordersModel->MapRead($order);
                    $data[$key]['order_name'] = '一生报告订单';
                    $order_data['title'] = '一生报告解锁';
                    $report_recode = $report['recode_data'] ? current($report['recode_data']) : [];
                    $game = [];
                    foreach ($report['goods_data'] as $acg)
                    {
                        $game[] = $acg['name'];
                    }
                    $map_count = count($game);
                    $gabe_sta = $game ? implode(',', $game) : '';
                    if ($map_count > 1)
                    {
                        $des = '开通' . implode(',', array_slice($game, 0, 1)) . '等' . $map_count . '篇';
                    } else
                    {
                        $des = '开通' . $gabe_sta;
                    }
                    $order_data['des1'] = '购买档案：' . $report_recode['name'] ?? '';
                    $order_data['des2'] = $des;
                    break;
                //重组视频订单
                case (int)$order['order_type'] === 6:
                    $video = $ordersModel->VideoRead($order);
                    $video_goods = $video['goods_data'] ? current($video['goods_data']) : [];
                    $data[$key]['order_name'] = '教学视频';
                    $data[$key]['video_id'] = $video['video_id'];
                    $order_data['title'] = $video_goods['name'];
                    $order_data['des1'] = $video['goods_data_des'];
                    break;
                //重组vip订单
                case (int)$order['order_type'] === 8:
                    $vip = $ordersModel->VipRead($order);
                    $vip_goods = $vip['goods_data'] ? current($vip['goods_data']) : [];
                    $data[$key]['order_name'] = '开通会员';
                    $order_data['title'] = '会员开通';
                    $order_data['des1'] = '开通时长：' . $vip_goods['name'];
                    //若会员购买成功后，显示有效期
                    if ((int)$order['status'] === 4)
                    {
                        $order_data['des2'] = '有效期至：' . $vip['vip_end_time'];
                    }
                    break;
                case (int)$order['order_type'] === 14: // 商城订单
                    $mall = $ordersModel->MallRead($order);
                    $data[$key]['order_name'] = '商城订单';
                    $order_data['title'] = $mall['goods_data']['name'] ?? '';
                    $order_data['des1'] = $mall['goods_data_des'] ?? '';
                    $img = $mall['goods_data']['imgs'] ? explode(',', $mall['goods_data']['imgs'])[0] : '';
                    $order_data['img'] = $img;
                    $data[$key]['gid'] = isset($mall['goods_data']['id']) ? (int)$mall['goods_data']['id'] : 0;
                    $data[$key]['sid'] = isset($mall['spec_info']['id']) ? (int)$mall['spec_info']['id'] : 0;
                    break;
                case (int)$order['order_type'] === 15:
                    $customReport = $ordersModel->CustomReport($order);
                    $data[$key]['order_name'] = '定制报告订单';
                    $order_data['title'] = '定制报告';
                    $order_data['des1'] = '购买档案：' . implode('&', array_column($customReport['recode_data'], 'name'));
                    $data[$key]['gid'] = (int)$customReport['question_data']['gid'];
                    break;
                case (int)$order['order_type'] === 16:
                    $extend = json_decode($order['extend'], true);
                    $data[$key]['order_name'] = '塔罗占卜订单';
                    $order_data['title'] = '塔罗占卜';
                    $order_data['des1'] = $extend['history']['content'];
                    $data[$key]['gid'] = (int)$order['gid'];
                    break;
                case (int)$order['order_type'] === 17:
                    $synastryReport = $ordersModel->SynastryReport($order);
                    $rname = array_column($synastryReport['recode_data'], 'name');
                    $goods = current($synastryReport['goods_data']);
                    $data[$key]['order_name'] = '恋爱合盘报告订单';
                    $extend = \GuzzleHttp\json_decode($order['extend'], true);
                    $data[$key]['srid'] = $extend['synastry_report']['srid'] ?? 1;
                    $order_data['title'] = config('synastryreport')[$data[$key]['srid']]['good_name'] ?? $goods['name'];
                    $order_data['des1'] = '开通档案：' . implode('&', $rname);
                    $order_data['des2'] = '当前关系：' . $synastryReport['relation_data']['name'];
                    $data[$key]['gid'] = (int)$order['gid'];
                    break;
                case (int)$order['order_type'] === 18: // 心理测试订单
                    $mental_test = $ordersModel->MentalTestDetail($order);
                    $data[$key]['order_name'] = '心理测试订单';
                    $order_data['title'] = $mental_test['name'];
                    $order_data['des1'] = $mental_test['des'];
                    $order_data['img'] = $mental_test['img'];
                    break;
                case (int)$order['order_type'] === 23: // 适合你的Ta

                    break;
                case (int)$order['order_type'] === 25: // 怎么追求TA

                    break;
                case (int)$order['order_type'] === 26: // 鉴渣神器

                    break;
            }
            if (isset($order_data) && $order_data)
            {
                $data[$key]['data'][] = $order_data;
            }
        }

        return $data;
    }
}
